package engine

import (
	"Crawlab/common/engine/os"
	"Crawlab/common/fetcher"
	"log"
)

// 启动引擎.
func Start(sends ...os.Request) {
	var copyRequestArray []os.Request

	// 将 sends 填充至 copyRequestArray.
	for _, singleRequest := range sends {
		copyRequestArray = append(copyRequestArray, singleRequest)
	}

	// 循环 copyRequestArray, 第一次是城市列表, 其他就是单个城市和单个用户的 循环.
	for 0 < len(copyRequestArray) {
		currentRequest := copyRequestArray[0]
		copyRequestArray = copyRequestArray[1:]

		// 提取器开始工作.
		bytes, err := fetcher.Start(currentRequest.Url)
		if nil != err {
			log.Printf("提取器工作异常, [Url: %s, Msg: %s]\n", currentRequest.Url, err)
		}

		// 对 Result 去重
		parseFuncResult := deduplication(currentRequest.ParseFunc(bytes))

		for _, item := range parseFuncResult.Items {
			log.Printf("Got item: %v\n", item)
		}

		copyRequestArray = append(copyRequestArray, parseFuncResult.Requests...)
	}
}

// 去重
func deduplication(parseFuncResult os.ParseResult) os.ParseResult {
	createItems := make([]interface{}, 0)
	createRequests := make([]os.Request, 0)

	for i := 0; i < len(parseFuncResult.Requests); i++ {
		repeat := false
		for j := i + 1; j < len(parseFuncResult.Requests); j++ {
			itemNameStringForOriginal := parseFuncResult.Items[i]
			itemNameStringForCompare := parseFuncResult.Items[j]

			if itemNameStringForCompare == itemNameStringForOriginal {
				repeat = true
				break
			}
		}
		if !repeat {
			createItems = append(createItems, parseFuncResult.Items[i])
			createRequests = append(createRequests, parseFuncResult.Requests[i])
		}
	}

	parseFuncResult.Requests = createRequests
	parseFuncResult.Items = createItems

	return parseFuncResult
}
